<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\common\model;
use think\Db;
use think\facade\Log;

class BookPrintLog extends Common
{
    //时间自动存储
    protected $autoWriteTimestamp = true;
    protected $updateTime = 'time';

    /**
     * @param $post
     *
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    {
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }

        $tableWhere = $this->tableWhere($post);
        $list = $this->field($tableWhere['field'])->where($tableWhere['where'])->order($tableWhere['order'])->paginate($limit);
        $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型

        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = $list->total();
        $re['data'] = $data;

        return $re;
    }

    //where搜索条件
    protected function tableWhere($post)
    {       
        $where = [];
        if(isset($post['func_type']) && $post['func_type'] != ""){
            $where[] = ['func_type', 'eq', $post['func_type']];
        }
        if(isset($post['operator']) && $post['operator'] != ""){
            $where[] = ['operator', 'like', '%'.$post['operator'].'%'];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = ['id ASC'];
        return $result;
    }

    // 添加日志
    public function addData($data)
    {
        $result = [
            'status' => false,
            'msg'    => '添加日志失败',
        ];

        if($this->allowField(true)->save($data))
        {
            $result['status'] = true;
            $result['msg']    = '添加日志成功';
        }
        return $result;
    }
}